package com.lgh.ibs.plugins.preferences;

import java.io.File;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.lgh.ibs.constants.ResourceFileHandler;
import com.lgh.ibs.constants.resources.SvnResources;
import com.lgh.ibs.log.IbsLogger;
import com.lgh.ibs.utils.Base64EncryptUtil;
import com.lgh.ibs.utils.PropertiesUtil;

public class SvnPreferencePage extends PreferencePage implements IWorkbenchPreferencePage{
	
	private Text t_svnUserName; //用户名
	private Text t_svnPassword; //密码框
	private Text t_svnPathVerifyRegex;
	PreferenceStore preferenceStore = null;
	public SvnPreferencePage() {
		super();
//		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		String fileName = SvnResources.SVN_properties.getFileName();
		File file = new File(fileName);
		if(!file.exists()){//若文件不存在，先初始化，避免报错
			IbsLogger.INFO("------------------->首选项中初始化文件目录!");
			ResourceFileHandler.init();
		}
		preferenceStore = new PreferenceStore(fileName);
		setPreferenceStore(preferenceStore);
		setDescription("这里配置svn账号信息!");
	}
	@Override
	public void init(IWorkbench workbench) {
		
	}
	//该方法为必须实现的方法，在此方法中创建页面上的各种控件 
	@Override
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE); 
		composite.setLayout(new GridLayout(2, false)); 
		try {
			String userName= PropertiesUtil.getValue(
					SvnResources.SVN_properties.getFileName(),  IbsPreferenceConstants.P_SVN_userName);
			String pwd= PropertiesUtil.getValue(
					SvnResources.SVN_properties.getFileName(), IbsPreferenceConstants.P_SVN_pwd);
			String regex= PropertiesUtil.getValue(
					SvnResources.SVN_properties.getFileName(), IbsPreferenceConstants.P_SVN_pathVerifyRegex);
			
			//获取保存此页面的PreferenceStore对象 
			//IPreferenceStore preferenceStore = getPreferenceStore(); 
//			IPreferenceStore preferenceStore = this.preferenceStore; 
			
			new Label(composite, SWT.LEFT).setText("登录用户名:"); 
			t_svnUserName = new Text(composite, SWT.BORDER); 
			t_svnUserName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 
			//设置用户名为保存在文件中的值 
			if(userName != null && userName !=""){
				t_svnUserName.setText(userName); 
			}
			
			new Label(composite, SWT.LEFT).setText("登录密码:"); 
			t_svnPassword = new Text(composite, SWT.BORDER); 
			t_svnPassword.setEchoChar('*'); //设置密码用*显示
			t_svnPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 
			//设置密码为保存在文件中的值 
			if(pwd != null && pwd !=""){
				pwd = Base64EncryptUtil.customDecipher(pwd);
				t_svnPassword.setText(pwd);
			}
			
			new Label(composite, SWT.LEFT).setText("验证正则:"); 
			t_svnPathVerifyRegex = new Text(composite, SWT.BORDER); 
			t_svnPathVerifyRegex.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 
			//设置正则为保存在文件中的值 
			if(regex != null && regex !=""){
				t_svnPathVerifyRegex.setText(regex); 
			}
		} catch (Exception e) {
			MessageDialog.openError(parent.getShell(), "warning", "首选 项读取SVN信息出错！"+e.getMessage());
			IbsLogger.ERROR(e.getMessage(),e); 
		}
	   return composite; 
	}
	
	/* 
	* 覆盖父类中的方法，但单击“恢复默认值”按钮时调用该方法 
	*/ 
	protected void performDefaults() { 
//	   IPreferenceStore preferenceStore = getPreferenceStore();
		IPreferenceStore preferenceStore = this.preferenceStore;
	   t_svnUserName.setText( preferenceStore.getDefaultString(IbsPreferenceConstants.P_SVN_userName)); 
	   t_svnPassword.setText( preferenceStore.getDefaultString(IbsPreferenceConstants.P_SVN_pwd)); 
	   t_svnPathVerifyRegex.setText( preferenceStore.getDefaultString(IbsPreferenceConstants.P_SVN_pathVerifyRegex)); 
	} 
	/* 
	* 覆盖父类中的方法，但单击“应用”按钮时调用该方法 
	*/ 
	public boolean performOk() { 
		try {
//	   IPreferenceStore preferenceStore = getPreferenceStore();
			IPreferenceStore preferenceStore = this.preferenceStore;
			if (t_svnUserName != null){
				preferenceStore.setValue(IbsPreferenceConstants.P_SVN_userName, t_svnUserName.getText()); 
			}
			if (t_svnPassword != null){
				String password = t_svnPassword.getText();
				String customEncrypt = Base64EncryptUtil.customEncrypt(password);
				preferenceStore.setValue(IbsPreferenceConstants.P_SVN_pwd, customEncrypt);
			}
			if (t_svnPathVerifyRegex != null){
				preferenceStore.setValue(IbsPreferenceConstants.P_SVN_pathVerifyRegex, t_svnPathVerifyRegex.getText());
			}
		} catch (Exception e) {
			MessageDialog.openError(t_svnUserName.getShell(), "warning", "首选 项保存SVN信息出错！"+e.getMessage());
			IbsLogger.ERROR(e.getMessage(),e);
		}
		
	   return true; 
	} 
	
	
	
	@Override
	public void setPreferenceStore(IPreferenceStore store) {
		super.setPreferenceStore(this.preferenceStore);
	}
	
	
	@Override
	public IPreferenceStore getPreferenceStore() {
		return this.preferenceStore;
	}
	@Override//用于扩展自己的按钮
	protected void contributeButtons(Composite parent) { 
	   // super.contributeButtons(parent); 
	  /* Button bt1 = new Button(parent, SWT.NONE); 
	   bt1.setText("按钮一"); 
	   ((GridLayout) parent.getLayout()).numColumns++; 
	   Button bt2 = new Button(parent, SWT.NONE); 
	   bt2.setText("按钮二"); 
	   ((GridLayout) parent.getLayout()).numColumns++; */
	} 
}